package edu.mit.media.fv.aqview
{
	import edu.mit.media.fv.aqwidgets.*;
	import edu.mit.media.fv.blocks.*;
	import edu.mit.media.fv.components.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.text.*;
	
	public class AQShoppingViewer extends AQViewer
	{
		//private var tags, store, duration, sender, date:AQTextField;
		//private var goodButton, badButton, switchButton, sendButton:AQCircleButton2;
		private var descriptors:Array;
		private var isViewingDescription:Boolean = true;
		private var choices:Array;//array of AQTextField
		private var viewport:Sprite;
		private var scroll:FVGlassScrollPane;
		
		public function AQShoppingViewer()
		{
			super();
			this.descriptors = [vTags, vStore, vDuration, vSender, vDate,
								tags, store, duration, sender, date];
			this.isViewingDescription = true;
			this.scroll = new FVGlassScrollPane(10,100,600,280);
			this.scroll.background = true;
			FluidEventHandler.addEventListener(DisplayEvent.OPEN_SHOPPING, displayWidget);
			
			this.goodButton.addEventListener(MouseEvent.CLICK, processGood);
			this.badButton.addEventListener(MouseEvent.CLICK, processGood);
			this.switchButton.addEventListener(MouseEvent.CLICK, switchView);
			this.sendButton.addEventListener(MouseEvent.CLICK, replyMessage);
		}
		public function switchView(event:MouseEvent):void{
			var i:int = 0;
			if(isViewingDescription){
				for(i = 0; i<this.descriptors.length; i++){
					this.removeChild(DisplayObject(this.descriptors[i]));
				}
				this.addChild(this.scroll);
			}else{
				for(i = 0; i<this.descriptors.length; i++){
					this.addChild(DisplayObject(this.descriptors[i]));
				}
				this.removeChild(this.scroll);
			}
			isViewingDescription = !isViewingDescription;
		}
		
		public function displayWidget(event:DisplayEvent):void{
			FluidEventHandler.dispatchEvent(new WidgetEvent(WidgetEvent.DISPLAY_WIDGET, this, "Shopping"));
			
			this.tags.text = event.subject;
			this.store.text = event.store;
			this.sender.text = event.sourceName;
			this.date.text = event.date;
			this.id = event.id;
			
			this.choices = new Array();
			this.viewport = new Sprite();
			for(var i:int = 0; i<event.choices.length; i++){
				var option:AQTextField = new AQTextField();
				option.width = 550;
				option.text = event.choices[i];
				option.editable = false;
				choices.push(option);
				this.viewport.addChild(option);
			}
			this.reformViewport();
			this.scroll.viewport = this.viewport;
			this.scroll.reform();
		}
		private function replyMessage(event:MouseEvent):void{
			var e:WidgetEvent = new WidgetEvent(WidgetEvent.DISPLAY_TEXT_BUILDER);
			e.subject = this.tags.text;
			e.id = this.id;
			e.text = "";
			FluidEventHandler.dispatchEvent(e);
		}
		private function sendMessage(event:MouseEvent = null):void{
			var data:Object = new Object();
			data.command = FluidConstants.REQUEST_SEND_OBJECT;
			var fvne:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(fvne);
			
			FluidEventHandler.dispatchEvent(new WidgetEvent(WidgetEvent.DISPLAY_WORKSPACE));
		}
		private function reformViewport():void{
			var maxY:int = 0;
			for(var i:int = 0; i<this.viewport.numChildren; i++){
				var c:DisplayObject = this.viewport.getChildAt(i);
				c.y = maxY;
				maxY =maxY+c.height;
			}
		}
		
	}
}